//
//  FixViewController.m
//  ZhiHuiLiJiang
//
//  Created by work on 2018/11/14.
//  Copyright © 2018年 王雅鹏. All rights reserved.
//设置VC

#import "FixViewController.h"
#import "FixOneCell.h"
#import "BYCleanCache.h"

@interface FixViewController ()<UITableViewDelegate,UITableViewDataSource>
/**tableView*/
@property (nonatomic ,strong) UITableView * tableView;
/**数据源数组*/
@property(nonatomic,strong)NSArray *titleDataArr;
/**缓存的值*/
@property(nonatomic,copy)NSString *settingCacheStr;

@end
@implementation FixViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatNavigationBar];
    [self getSettingTitleArr];
    [self.view addSubview:self.tableView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (!self.tabBarController.tabBar.isHidden) {
        self.tabBarController.tabBar.hidden = YES;
    }
}
- (void)creatNavigationBar{
    BY_kWeakSelf(ws);
    self.view.backgroundColor = [UIColor whiteColor];
    [self setTitleStr:@"设置" leftAction:^{
        [ws.navigationController popViewControllerAnimated:YES];
    }];
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, BY_Nav_H, KSCREEWIDTH, KSCREEHEGIHT-BY_NavBot_H) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = BY_Height(100);
        _tableView.tableFooterView = [UIView new];
        [_tableView registerClass:[FixOneCell class] forCellReuseIdentifier: @"settingCellId"] ;
    }
    return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titleDataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FixOneCell *cell = [tableView dequeueReusableCellWithIdentifier:@"settingCellId" forIndexPath:indexPath];
    if (_titleDataArr.count) {
        cell.titleStr = _titleDataArr[indexPath.row];
    }
    if ([_titleDataArr[indexPath.row] isEqualToString:@"清除缓存"]) {
        cell.cacheStr = _settingCacheStr;
    }
    if ([_titleDataArr[indexPath.row] isEqualToString:@"清除缓存"]||[_titleDataArr[indexPath.row] isEqualToString:@"注销账号"]) {
        cell.arrowIcon.hidden = YES;
    }else{
        cell.arrowIcon.hidden = NO;
    }
    return cell;
}

#pragma mark- 数据处理
- (NSArray *)titleDataArr{
    if (!_titleDataArr) {
        _titleDataArr = [NSArray array];
    }
    return _titleDataArr;
}
- (NSString *)settingCacheStr{
    if (!_settingCacheStr) {
        _settingCacheStr = @"";
    }
    return _settingCacheStr;
}
- (void)getSettingTitleArr{
    if (![[SaveCacheData loadDataStrWithKey:loginState] isEqualToString:@"YES"]) {//如果没有登录
        self.titleDataArr = @[@"隐私政策",@"清除缓存",@"应用版本信息说明"];
    }else{
        self.titleDataArr = @[@"隐私政策",@"清除缓存",@"应用版本信息说明",@"注销账号"];
    }
    self.settingCacheStr = [NSString stringWithFormat:@"%0.2fMB",[BYCleanCache folderSizeAtPath]];
}

#pragma mark- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.row == 0 ) {//隐私政策
        for (BYSetModel *models in APPDELEGATE.SetDataArray) {
            if ([models.setting_name isEqualToString:@"sys-privacypolicy-url"]) {
                NSString *urlStr = models.setting_value;
                BYWebVC *VC = [[BYWebVC alloc]init];
                VC.url = urlStr;
                [self.navigationController pushViewController:VC animated:YES];
            }
        }
    }else if (indexPath.row == 1) {
        [self AlertWithTitle:@"确定清空缓存？" message:nil andOthers:@[@"取消",@"确定"] animated:YES action:^(NSInteger index) {
            if (index == 1) {
                [BYCleanCache cleanCache:^{
                    self.settingCacheStr = [NSString stringWithFormat:@"%0.2fMB",[BYCleanCache folderSizeAtPath]];
                    [self.tableView reloadData];
                }];
            }
        }];
    }else if (indexPath.row == 2) {//应用版本信息说明
        for (BYSetModel *models in APPDELEGATE.SetDataArray) {
            if ([models.setting_name isEqualToString:@"sys-versiondesc-url"]) {
                NSString *urlStr = models.setting_value;
                BYWebVC *VC = [[BYWebVC alloc]init];
                NSString *webURL= @"";
                NSString *paramStr = @"ver=ios";
                if ([urlStr rangeOfString:@"?"].location == NSNotFound) {//没有?参数
                    webURL = [NSString stringWithFormat:@"%@?%@",urlStr,paramStr];
                } else {//有参数
                    webURL = [NSString stringWithFormat:@"%@&%@",urlStr,paramStr];
                }
                VC.url = webURL;
                [self.navigationController pushViewController:VC animated:YES];
            }
        }
    }else if (indexPath.row == 3 ) {//退出登录
         [self AlertWithTitle:@"确定注销账号？" message:nil andOthers:@[@"取消",@"确定"] animated:YES action:^(NSInteger index) {
             if (index == 1) {
                 [self requestLogoutAccount];
             }
         }];
    }else{
        
    }
}

#pragma mark- 网络请求
- (void)requestLogoutAccount{
    [userManager requestlogout:@{@"Authorization":[SaveCacheData loadDataStrWithKey:currentUserToken]} logout:^(BOOL success, NSString * _Nonnull msg, id  _Nonnull BODY) {
        if (success) {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}


@end
